package ${entity.getSiblingPackage("service.impl")};

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import ${entity.getSiblingPackage("dto")}.${entity.entityName}Dto;
import ${entity.entityClass};
import ${entity.getSiblingPackage("repository")}.${entity.entityName}Repository;
import ${entity.getSiblingPackage("service")}.${entity.entityName}Service;
import wo.common.entity.WoPage;
import wo.common.util.WoUtil;
#if($entity.isWeak())
import ${entity.weakRelationM21.target.entityClass};
#end

/**
 * PO实体${entity.entityName}对应的Service接口实现类.
 * @author cailei
 */
@Service
@Transactional
public class ${entity.entityName}ServiceImpl implements ${entity.entityName}Service {

	/**
	 * 使用日志打印框架.
	 */
	private final static Logger LOG = LogManager.getLogger(${entity.entityName}ServiceImpl.class);

	/**
	 * 注入${entity.entityName}Repository.
	 */
	@Resource // @Autowired
	private ${entity.entityName}Repository ${entity.entityNameLowerFirstChar}Repository;

#if($entity.isWeak())
	/**
	 * DataTable获取${entity.entityCN}列表数据.
	 * 
	 * @param start 当前页开始索引，从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @param ${entity.weakRelationM21.sourceFieldName}Id ${entity.weakRelationM21.sourceFieldCN}Id
	 * @return
	 */
	@Override
	public WoPage<${entity.entityName}Dto> getPageData(Long start, Long length, String searchContent, String dir,
		${entity.weakRelationM21.target.firstKeyField.fieldType.name} ${entity.weakRelationM21.sourceFieldName}Id) {
		// 构造查询条件
		ExampleMatcher m = ExampleMatcher.matching();
		// 构造查询对象
		${entity.entityName} qo = new ${entity.entityName}();
		// 设置条件：${entity.entityName}.${entity.weakRelationM21.sourceFieldName}.${entity.weakRelationM21.target.firstKeyField.fieldName} = ${entity.weakRelationM21.sourceFieldName}Id
		if (!WoUtil.isEmpty(${entity.weakRelationM21.sourceFieldName}Id)) {
			m.withMatcher("${entity.weakRelationM21.sourceFieldName}.${entity.weakRelationM21.target.firstKeyField.fieldName}", GenericPropertyMatchers.exact());
			${entity.weakRelationM21.target.entityClass} rObj = new ${entity.weakRelationM21.target.entityClass}();
			rObj.setId(${entity.weakRelationM21.sourceFieldName}Id);
			qo.set${entity.weakRelationM21.sourceFieldNameUpperFirstChar}(rObj);
		}
		// 设置条件：${entity.entityName}.${entity.firstColumnField.fieldName} contains searchContent
		if (!WoUtil.isEmpty(searchContent)) {
			m.withMatcher("${entity.firstColumnField.fieldName}", GenericPropertyMatchers.contains());
			qo.set${entity.firstColumnField.fieldNameUpperFirstChar}(searchContent);
		}
		// 构造Example将查询条件组合起来（条件之间是逻辑与的关系）
		Example<${entity.entityName}> ex = Example.of(qo, m); 
		// 构造分页和排序对象
		Pageable pageInput = PageRequest.of(start.intValue() / length.intValue(), length.intValue());
		// 查询最终分页数据
		Page<${entity.entityName}> pageData = ${entity.entityNameLowerFirstChar}Repository.findAll(ex, pageInput);
		// 将PO转化为DTO
		WoPage<${entity.entityName}Dto> pr = ${entity.entityName}Dto.getPageData(pageData.getContent(), pageData.getTotalElements());
		return pr;
	}
#else
	/**
	 * DataTable获取${entity.entityCN}列表数据.
	 * @param start 当前页开始索引,从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @return
	 */
	@Override
	public WoPage<${entity.entityName}Dto> getPageData(Long start, Long length, String searchContent, String dir) {
		// 从dao获取数据
		Pageable pageInput = PageRequest.of(start.intValue() / length.intValue(), length.intValue()
			#if (${entity.isOrderable()}) , "desc".equals(dir) ? Direction.DESC : Direction.ASC, "${entity.orderableField.fieldName}" #end );
		Page<${entity.entityName}> pageData = null;
		if (WoUtil.isEmpty(searchContent)) {
			pageData = ${entity.entityNameLowerFirstChar}Repository.findAll(pageInput);
		} else {
			pageData = ${entity.entityNameLowerFirstChar}Repository.findAllBy${entity.firstColumnField.fieldName}Like("%" + searchContent + "%", pageInput);
		}
		// 将PO转化为DTO
		WoPage<${entity.entityName}Dto> pr = ${entity.entityName}Dto.getPageData(pageData.getContent(), pageData.getTotalElements());
		return pr;
	}
#end

	/**
	 * 创建${entity.entityCN}
	 * @param dto
	 */
	@Override
	public void create(${entity.entityName}Dto dto) {
		${entity.entityName} po = dto.createPO();
		${entity.entityNameLowerFirstChar}Repository.save(po);
	}

	/**
	 * 根据id获取${entity.entityCN}数据
	 * @param id
	 * @return
	 */
	@Override
	public ${entity.entityName}Dto getById(String id) {
		${entity.entityName} po = ${entity.entityNameLowerFirstChar}Repository.findById(id).get();
		return new ${entity.entityName}Dto (po);
	}

	/**
	 * 修改${entity.entityCN}
	 * @param dto
	 */
	@Override
	public void update(${entity.entityName}Dto dto) {
		${entity.entityName} po = ${entity.entityNameLowerFirstChar}Repository.findById(dto.getId()).get();
		dto.updatePO(po);
	}

	/**
	 * 根据id数组删除一个或者多个${entity.entityCN}
	 * @param id
	 */
	@Override
	public void delete(String[] id) {
		${entity.entityNameLowerFirstChar}Repository.deleteByIdIn(id);
	}

}
